﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Connect
{
    public partial class Restore : Form
    {
        public Restore()
        {
            InitializeComponent();
            cn = tempcn.GetConnect();
        }
        Connection tempcn = new Connection();
        SqlConnection cn = new SqlConnection();

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(lin) == true)
                {
                    return;
                }
                else
                {
                    Connection h = new Connection();
                    cn.Open();
                    lin = cnSQL.Restore(opendg, cn.Database.ToString());
                    SqlCommand cm1 = new SqlCommand();
                    System.Data.SqlClient.SqlConnection.ClearAllPools();
                    cm1.CommandText = lin;
                    SqlConnection con = new SqlConnection("server=" + h.rosv() + ";database=master;Integrated Security=True");
                    con.Open();
                    cm1.Connection = con;
                    cm1.ExecuteNonQuery();
                    BackgroundWorker worker = sender as BackgroundWorker;
                    MessageBox.Show("Phục hồi CSDL thành công", "Thành công");
                }

                cancel_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Phục hồi thất bại : " + ex.Message, "Thất bại");
            }
        }
        OpenFileDialog opendg = new OpenFileDialog();
        private void cancel_Click(object sender, EventArgs e)
        {
            try
            {
                Close();
            }
            catch (Exception)
            {

                Application.ExitThread();
            }
        }
        string lin = "";
        private void res_Click(object sender, EventArgs e)
        {
            opendg.Filter = "Tập tin sao lưu (*.bak)|*.bak|Tất cả các dạng tệp tin (*.*)|*.*";
            opendg.AddExtension = true;
            opendg.DefaultExt = "bak";
            opendg.RestoreDirectory = true;
            opendg.InitialDirectory = "D:\\";
            opendg.Title = "Phuc hoi tap tin sao luu tai cac thoi diem khac nhau";
            opendg.FilterIndex = 1;
            if (opendg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                lin = opendg.FileName.Trim();
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
   progressBar1.Value = 100;
        }


    }
}
